package com.firebird.app.fekpsl.ui.activity.me

import android.view.View
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.base.BaseMvpActivity
import com.firebird.app.fekpsl.constant.Constant
import com.firebird.app.fekpsl.mvp.me.RealNameContract
import com.firebird.app.fekpsl.mvp.me.RealNamePresenter
import com.firebird.app.fekpsl.utils.Preference
import kotlinx.android.synthetic.main.me_update_real_name.*
import kotlinx.android.synthetic.main.toolbar.*

class UpdateRealNameActivity  : BaseMvpActivity<RealNameContract.View, RealNameContract.Presenter>(), RealNameContract.View {
    private var pf_real_name:String by Preference(Constant.REAL_NAME_KEY, "")
    private var pf_uuid:String by Preference(Constant.UUID_KEY, "")

    override fun createPresenter(): RealNameContract.Presenter = RealNamePresenter()

    private val mDialog by lazy {
        // DialogUtil.getWaitDialog(this, getString(R.string.save_ing))
    }

    override fun showLoading() {
        // mDialog.show()
    }

    override fun hideLoading() {
        // mDialog.dismiss()
    }

    override fun updateRealNameSuccess(success: Boolean) {
        if(success){
            pf_real_name=et_me_real_name.text.toString()
            finish()
        }else{
            // showToast(getString(R.string.me_save_fail))
        }
    }
    /*
        override fun updateNiceNameFail() {
            showToast(getString(R.string.me_save_fail))
        }*/
    override fun attachLayoutRes(): Int = R.layout.me_update_real_name

    override fun initData() {
    }

    override fun initView() {
        super.initView()

        toolbar.run {
            title = resources.getString(R.string.me_update_real_name)
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
        et_me_real_name.setText(pf_real_name)
        btn_save.setOnClickListener(onClickListener)
    }

    override fun start() {
    }

    /**
     * OnClickListener
     */
    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.btn_save -> {
                updateRealName()
            }
        }
    }
    private fun updateRealName() {
        if (validate()) {
            mPresenter?.updateRealName(pf_uuid,
                    et_me_real_name.text.toString()
            )
        }
    }
    /**
     * check data
     */
    private fun validate(): Boolean {
        var valid = true
        val realName: String = et_me_real_name.text.toString()

        if (realName.isEmpty()) {
            et_me_real_name.error = getString(R.string.me_real_name_not_empty)
            valid = false
        }
        if (realName.length>20){
            et_me_real_name.error = getString(R.string.me_real_name_len_error)
            valid = false
        }
        return valid
    }
}